﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

namespace System.Windows.Forms
{
    /// <summary>
    /// 表示信号灯标签
    /// </summary>
    /// <creator>marc</creator>
    public class SignalLabel : Label
    {
        private bool _isTurnOn = false;
        private Color _signalColor = Color.Lime;
        private Color _turnOffColor = Color.Gray;

        /// <inheritdoc cref="SignalLabel"/>
        public SignalLabel()
        {
            Size = new Size(Width, 26);
            AutoSize = false;
        }

        /// <summary>
        /// 是否打开
        /// </summary>
        [Browsable(true)]
        [Category("Xejen")]
        [DefaultValue(false)]
        [Description("是否打开")]
        public bool IsTurnOn
        {
            get
            {
                return _isTurnOn;
            }
            set
            {
                _isTurnOn = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 信号颜色
        /// </summary>
        [Browsable(true)]
        [Category("Xejen")]
        [DefaultValue(typeof(Color), "#FF00FF00")]
        [Description("亮灯时的信号颜色")]
        public Color SignalColor
        {
            get
            {
                return _signalColor;
            }
            set
            {
                _signalColor = value;
                Invalidate();
            }
        }

        /// <summary>
        /// 关闭时颜色
        /// </summary>
        [Browsable(true)]
        [Category("Xejen")]
        [DefaultValue(typeof(Color), "#FF808080")]
        [Description("灯灭时的信号颜色")]
        public Color TurnOffColor
        {
            get
            {
                return _turnOffColor;
            }
            set
            {
                _turnOffColor = value;
                Invalidate();
            }
        }

        /// <inheritdoc/>
        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            float lightPadding = 1.5f;
            var lightHeight = Height - 2 * lightPadding;

            var lightColor = IsTurnOn ? SignalColor : TurnOffColor;
            using (Brush signalBrush = new SolidBrush(lightColor))
            {
                e.Graphics.FillEllipse(signalBrush, lightPadding, lightPadding, lightHeight, lightHeight);
            }
            using (Pen signalPen = new Pen(Color.Black, 1.5f))
            {
                e.Graphics.DrawEllipse(signalPen, lightPadding, lightPadding, lightHeight, lightHeight);
            }

            e.Graphics.SmoothingMode = SmoothingMode.None;
            using (Brush textBrush = new SolidBrush(ForeColor))
            {
                using (StringFormat format = new StringFormat())
                {
                    format.LineAlignment = StringAlignment.Center;
                    e.Graphics.DrawString(Text, Font, textBrush, new RectangleF(Height, 0, Width - Height, Height), format);
                }
            }
        }
    }

}
